//
//  EPMNetworkTool.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/4/30.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMNetworkTool.h"
#import <AFNetworking/AFNetworking.h>

#pragma mark - 网址 -


@protocol HTTPProxy <NSObject>
@optional
- (NSURLSessionDataTask*)dataTaskWithHTTPMethod:(NSString *)method
                                      URLString:(NSString *)URLString
                                     parameters:(id)parameters
                                 uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                               downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                        success:(void (^)(NSURLSessionDataTask *, id))success
                                        failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end
@interface EPMNetworkTool()<HTTPProxy>

@end
@implementation EPMNetworkTool
+ (instancetype)sheadTool{
    
    static EPMNetworkTool* _instance;
    static dispatch_once_t token;
    dispatch_once (&token,^{
        _instance = [self manager];
         _instance.responseSerializer.acceptableContentTypes = [_instance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        _instance.requestSerializer = [AFJSONRequestSerializer serializer];
    });
    return _instance;
}

#pragma mark - 数据请求方法 -

- (void)request:(AFNRequestMethod)method url:(NSString *)urlString paramater:(id)paramater finisedBlock:(void (^)(id, NSError *))finishedBlock
{
    NSString* methodName =  ( method == GET ? @"GET" : @"POST");
    
    //成功回调
    void(^succesBlock)(NSURLSessionTask*, id) = ^ (NSURLSessionTask* task, id respond){
        
        finishedBlock( respond , nil);
    };
    //失败回调
    
    void(^failBlock)(NSURLSessionTask* , NSError*) = ^ (NSURLSessionTask* task, NSError* error){
        finishedBlock(nil,error);
    };
    
    [[self dataTaskWithHTTPMethod:methodName URLString:urlString parameters:paramater uploadProgress:nil downloadProgress:nil success:succesBlock failure:failBlock]resume];
}

#pragma mark - 首页数据 -iosapi.itcast.cn/loveBeen/focus.json.php

- (void)getHomePageDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
 
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/focus.json.php";
    NSDictionary* para = @{
                           @"call":@(1)
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}

#pragma mark - 新鲜热卖 -

- (void)getHotSallDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/firstSell.json.php";
    NSDictionary* para = @{
                           @"call":@"2"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}

#pragma mark - 程序启动广告 -

- (void)getStartADDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/ad.json.php";
    NSDictionary* para = @{
                           @"call":@"7"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}

#pragma mark - 闪电超市 -

- (void)getSuperMarketDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/supermarket.json.php";
    NSDictionary* para = @{
                           @"call":@"5"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}

#pragma mark - 搜索关键词 -

- (void)getSearchKeywordDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/search.json.php";
    NSDictionary* para = @{
                           @"call":@"6"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}


#pragma mark - 搜索返回数据 -

- (void)getSearchReasultDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/promotion.json.php";
    NSDictionary* para = @{
                           @"call":@"8"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}

#pragma mark - 我的订单 -

- (void)getMyOrderDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/MyOrders.json.php";
    NSDictionary* para = @{
                           @"call":@"13"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}

#pragma mark - 我的优惠券 -

- (void)getMyCouponDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/MyCoupon.json.php";
    NSDictionary* para = @{
                           @"call":@"9"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}

#pragma mark - 系统消息 -

- (void)getSystemMessageDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/SystemMessage.json.php";
    NSDictionary* para = @{
                           @"call":@"10"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}

#pragma mark - 我的消息 -

- (void)getMyMessageDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/UserMessage.json.php";
    NSDictionary* para = @{
                           @"call":@"11"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}

#pragma mark - 收货地址 -

- (void)getMyAddressDatafinisedBlock:(void (^)(id, NSError *))finishedBlock{
    AFNRequestMethod metod = POST;
    NSString* homePageUrl = @"http://iosapi.itcast.cn/loveBeen/MyAdress.json.php";
    NSDictionary* para = @{
                           @"call":@"12"
                           };
    
    [self request:metod url:homePageUrl paramater:para finisedBlock:finishedBlock];
}


@end
